package cn.iocoder.yudao.module.promotion.gift.dao;

import cn.iocoder.yudao.framework.business.basic.dao.mapper.CoreMapper;
import cn.iocoder.yudao.framework.business.basic.dao.query.CoreLambdaQueryWrapper;
import cn.iocoder.yudao.framework.datapermission.core.annotation.DataPermission;
import cn.iocoder.yudao.module.promotion.gift.pojo.po.GiftActivity;
import jakarta.annotation.Nullable;
import org.apache.ibatis.annotations.Mapper;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <pre>
 * OOoO0OOoO0OOOooo0oOOOO0OOOOO0oooOO0ooOOO0Ooooo0OOOOo0ooooO0OOooo0Ooooo0OOOOO
 *  这里是文件说明
 * OOoO0OOoO0OOOooo0oOOOO0OOOOO0oooOO0ooOOO0Ooooo0OOOOo0ooooO0OOooo0Ooooo0OOOOO
 * </pre>
 *
 * @author 山野羡民（1032694760@qq.com）
 * @since 2025/01/13
 */
@Mapper
public interface GiftActivityMapper extends CoreMapper<GiftActivity> {

    @DataPermission(enable = false)
    default List<GiftActivity> selectAvailableListByShopId(Long shopId, @Nullable Integer limit) {
        CoreLambdaQueryWrapper<GiftActivity> wrapper = new CoreLambdaQueryWrapper<>();
        wrapper.gt(GiftActivity::getEndTime, LocalDateTime.now());
        wrapper.apply("FIND_IN_SET({0}, partner_shop_ids) > 0", shopId);
        wrapper.last(" ORDER BY RAND()");
        if (limit != null && limit > 0) {
            wrapper.limitN(limit);
        }
        return selectList(wrapper);
    }

}
